package com.ysten.basic.common.log.config;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

public enum PropertyLogConfiguration
{
    INSTANCE;
    
    private static Logger logger = LoggerFactory.getLogger(PropertyLogConfiguration.class);
    
    private Map<String, List<String>> configuration = null;
    
    public Map<String, List<String>> getConfiguration()
    {
        if (null == configuration)
        {
            configuration = Maps.newHashMap();
            StringBuilder sb = new StringBuilder();
            sb.append("propertylog").append(System.getProperty("file.separator")).append("propertylog.properties");
            configuration = loadPrpoertiesMap(sb.toString());
        }
        
        return configuration;
    }
    
    /**
     * <构建配置Map>
     * 
     * @param configFileName
     * @return
     * @see [类、类#方法、类#成员]
     */
    private Map<String, List<String>> loadPrpoertiesMap(String configFileName)
    {
        logger.debug("Enter func[buildPrpoertiesMap], load file to map.");
        
        Map<String, List<String>> propertiesMap = Maps.newHashMap();
        InputStream in = null;
        Properties prop = null;
        try
        {
            ClassLoader clToUse = ClassUtils.getDefaultClassLoader();
            URL url = clToUse.getResource(configFileName);
            URLConnection con = url.openConnection();
            in = con.getInputStream();
            prop = new Properties();
            prop.load(in);
            
            for (Entry<Object, Object> entry : prop.entrySet())
            {
                String key = (String)entry.getKey();
                String value = (String)entry.getValue();
                List<String> valueList = Lists.newArrayList(StringUtils.commaDelimitedListToStringArray(value));
                propertiesMap.put(key, valueList);
            }
        }
        catch (Exception e)
        {
            logger.error("Inner func[buildPrpoertiesMap], catch Exception while load configFile.", e);
        }
        finally
        {
            if (in != null)
            {
                try
                {
                    in.close();
                }
                catch (IOException e)
                {
                    logger.error("Inner func[buildPrpoertiesMap], catch IOException while close InputStream.", e);
                }
            }
        }
        
        logger.debug("Exit func[buildPrpoertiesMap], load file to map result", new Object[] {propertiesMap});
        return propertiesMap;
    }
}
